import unittest
from unittest.mock import MagicMock, patch

from qmdr_tts import TTSClient, TTSConfig


class TestTTSClient(unittest.TestCase):
    def setUp(self):
        self.tts_client = TTSClient()
        self.config = TTSConfig()

    @patch("qmdr_tts.tts.requests.Session.post")
    @patch("qmdr_tts.tts.requests.Session.get")
    def test_text_to_speech(self, mock_get, mock_post):
        # 模拟POST返回流式NDJSON
        mock_response = MagicMock()
        mock_response.iter_lines.return_value = [
            b'{"code":200,"data":{"tts":"http://fake/audio1.mp3"}}',
            b'{"code":200,"data":{"tts":"http://fake/audio2.mp3"}}',
        ]
        mock_response.raise_for_status.return_value = None
        mock_post.return_value = mock_response

        # 模拟GET返回音频内容
        mock_audio_response = MagicMock()
        mock_audio_response.content = b"fake-audio-data"
        mock_audio_response.raise_for_status.return_value = None
        mock_get.return_value = mock_audio_response

        # 执行方法
        result = self.tts_client.text_to_speech(
            text="测试语音", output_path="output/test.mp3", config=self.config
        )
        self.assertTrue(result)


if __name__ == "__main__":
    unittest.main()
